﻿// Decompiled with JetBrains decompiler
// Type: LayersParser.Objects.Layers
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using LayersParser.Extensions;
using LayersParser.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Linq;


namespace LayersParser.Objects;

public class Layers : Common
{
  private List<IfCondition> conditions = new List<IfCondition>();
  private List<string> _params = new List<string>();
  private string detailledcommand = "";
  private List<Line> multiline = new List<Line>();
  private List<Line> multilineBuffer = new List<Line>();
  private List<string> dynamicParameters;

  public Layers()
  {
  }

  public Layers(XElement layernode, string backupText)
  {
    this.Node = layernode;
    string[] separator = new string[1]{ "\n" };
    foreach (string line1 in backupText.Split(separator, StringSplitOptions.RemoveEmptyEntries))
    {
      if (!line1.IsJustWhiteSpace())
      {
        XElement content = new XElement((XName) "Line");
        Line line2 = new Line();
        line2.Items.Add((ISublines) new Item("const")
        {
          Value = line1
        });
        this.multiline.Add(line2);
        content.Add((object) line2.Items[0].SublineNode);
        line2.Node = content;
        this.Node.Add((object) content);
        this.detailledcommand = $"{this.detailledcommand}{line1}\n";
      }
    }
  }

  public Layers(XElement layernode)
  {
    this.Node = layernode;
    List<XElement> xelementList1 = new List<XElement>();
    XElement node = this.Node;
    List<XElement> xelementList2;
    if (node == null)
    {
      xelementList2 = (List<XElement>) null;
    }
    else
    {
      IEnumerable<XElement> source = node.Elements((XName) "Line");
      xelementList2 = source != null ? source.ToList<XElement>() : (List<XElement>) null;
    }
    List<XElement> xelementList3 = xelementList2;
    int index = 0;
    foreach (XElement xelement in xelementList3)
    {
      this.multiline.Add(new Line());
      if (xelement.Element((XName) "Item") != null)
      {
        foreach (XElement element in xelement.Elements())
        {
          if (element.Name == (XName) "Item")
          {
            this.multiline[index].Items.Add((ISublines) new Item(element));
            this.DetailledCommand += element.Value;
          }
          else if (element.Name == (XName) "If")
          {
            IfCondition ifCondition = new IfCondition(element);
            this.DetailledCommand = ifCondition.Functions == null ? this.DetailledCommand + "Blank output" : this.DetailledCommand + ifCondition.Functions;
            this.conditions.Add(ifCondition);
            this.multiline[index].Items.Add((ISublines) ifCondition);
          }
          else if (element.Name == (XName) "ElseIf" || element.Name == (XName) "Else")
            ((IfCondition) this.multiline[index].Items[this.multiline[index].Items.Count - 1]).ElseIfConditionCtor(element);
        }
        this.DetailledCommand += "\n";
      }
      ++index;
    }
  }

  public List<string> DynamicParameters
  {
    get => this.dynamicParameters;
    set => this.dynamicParameters = value;
  }

  public List<Line> Multiline
  {
    get => this.multiline;
    set => this.multiline = value;
  }

  public string DetailledCommand
  {
    get => this.detailledcommand;
    set => this.detailledcommand = value;
  }

  public List<string> Params
  {
    get => this._params;
    set => this._params = value;
  }

  public List<IfCondition> Conditions
  {
    get => this.conditions;
    set => this.conditions = value;
  }

  public ISublines SetItem(string value, int index)
  {
    if (this.multiline.Count > index)
    {
      foreach (ISublines sublines in this.multiline[index].Items)
      {
        if (sublines.Value == value)
          return sublines;
      }
    }
    return (ISublines) null;
  }

  public void ChangeItems(int startIndex, string lastText, string newText)
  {
    int index1 = 0;
    foreach (Line line1 in this.multiline)
    {
      int index2 = 0;
      int index3 = 0;
      while (index3 < line1.Items.Count)
      {
        if (line1.Items[index3].Bounds[0] == line1.Items[index3].Bounds[1])
          this.multiline[index1].Items.RemoveAt(index3);
        else
          ++index3;
      }
      for (; index2 < line1.Items.Count; ++index2)
      {
        int bound1 = line1.Items[index2].Bounds[0];
        int bound2 = line1.Items[index2].Bounds[1];
        if (startIndex <= bound2 && startIndex > bound1)
        {
          string line2;
          if (line1.Items[index2].Value == newText)
            line2 = newText;
          else if (line1.Items[index2].Value == lastText)
            line2 = newText;
          else if (line1.Items[index2].Value.Length - lastText.Length < startIndex)
          {
            int length = line1.Items[index2].Value.Length - lastText.Length;
            line2 = line1.Items[index2].Value.Substring(0, length) + newText;
          }
          else
          {
            int length = startIndex - bound1;
            line2 = length + lastText.Length <= line1.Items[index2].Value.Length ? line1.Items[index2].Value.Substring(0, length) + newText + line1.Items[index2].Value.Substring(length + lastText.Length) : line1.Items[index2].Value.Substring(0, length - 1) + newText + line1.Items[index2].Value.Substring(length + lastText.Length - 1);
          }
          string type = line1.Items[index2].ReturnConditionType();
          this.multiline[index1].Items.RemoveAt(index2);
          using (List<string>.Enumerator enumerator = line2.SeparateItems(this._params, this.dynamicParameters).GetEnumerator())
          {
            while (enumerator.MoveNext())
            {
              string current = enumerator.Current;
              if (current == newText && this._params.Contains(newText))
              {
                ISublines sublines = (ISublines) new Item("parameter")
                {
                  Value = current
                };
                this.multiline[index1].Items.Insert(index2, sublines);
              }
              else if (current == newText && !this._params.Contains(newText))
              {
                ISublines sublines = (ISublines) new Item("dynamicParameter")
                {
                  Value = current
                };
                this.multiline[index1].Items.Insert(index2, sublines);
              }
              else
              {
                ISublines sublines = (ISublines) new Item(type)
                {
                  Value = current
                };
                this.multiline[index1].Items.Insert(index2, sublines);
              }
              ++index2;
            }
            break;
          }
        }
      }
      ++index1;
    }
    this.Concat();
    this.ResetDetailledCommandAndNode();
  }

  public void ResetDetailledCommandAndNode()
  {
    this.detailledcommand = "";
    this.Node.Elements((XName) "Line").Remove<XElement>();
    foreach (Line line in this.multiline)
    {
      XElement content = new XElement((XName) "Line");
      foreach (ISublines sublines in line.Items)
      {
        this.detailledcommand += sublines.Value;
        content.Add((object) sublines.SublineNode);
      }
      this.detailledcommand += "\n";
      this.Node.Add((object) content);
    }
  }

  public void SetNewConst(RichTextBox richTextBox)
  {
    int lineindex = 0;
    foreach (string line in richTextBox.Lines)
    {
      if (line != "" && lineindex < this.multiline.Count)
        this.LineDissection(line, lineindex);
      else if (line != "" && lineindex >= this.multiline.Count)
        this.multiline.Add(new Line()
        {
          Items = {
            (ISublines) new Item("const") { Value = line }
          }
        });
      ++lineindex;
    }
    this.Concat();
    this.ResetDetailledCommandAndNode();
  }

  public void SetAsConst(int index)
  {
    foreach (Line line in this.multiline)
    {
      foreach (ISublines sublines in line.Items)
      {
        if (index < sublines.Bounds[1] && index >= sublines.Bounds[1])
          ((Item) sublines).Type = "const";
      }
    }
  }

  private List<string> RemoveWhiteSpace(List<string> strings)
  {
    List<string> stringList = new List<string>();
    foreach (string str in strings)
    {
      if (str != " ")
        stringList.Add(str);
    }
    return stringList;
  }

  private void LineDissection(string line, int lineindex)
  {
    string[] separator1 = this.multiline[lineindex].Items.ISUblinesToStr().CreateSplitting().RemoveVoid();
    string[] strArray = line.Split(separator1, StringSplitOptions.None).RemoveVoid();
    if (strArray.Length == 0)
      return;
    Line line1 = new Line();
    string str = line;
    foreach (ISublines sublines in this.multiline[lineindex].Items)
    {
      if (str.Replace(sublines.Value, "").Length != str.Length && str.IndexOf(sublines.Value) == 0)
      {
        line1.Items.Add(sublines);
        str = str.Substring(sublines.Value.Length);
      }
      else if (str.Replace(sublines.Value, "").Length != str.Length && str.IndexOf(sublines.Value) != 0)
      {
        line1.Items.Add((ISublines) new Item("const")
        {
          Value = strArray[0]
        });
        line1.Items.Add(sublines);
        string[] separator2 = new string[1]
        {
          sublines.Value
        };
        if (str.Split(separator2, StringSplitOptions.None).Length > 1)
          str = str.Split(separator2, StringSplitOptions.None)[1];
      }
      else if (str.Replace(sublines.Value, "").Length == str.Length)
      {
        line1.Items.Add((ISublines) new Item("const")
        {
          Value = strArray[0]
        });
        str = str.Replace(strArray[0], "");
      }
      if (sublines == this.multiline[lineindex].Items[this.multiline[lineindex].Items.Count - 1] && str.Length != 0)
      {
        line1.Items.Add((ISublines) new Item("const")
        {
          Value = strArray[0]
        });
        str = str.Replace(strArray[0], "");
      }
    }
    this.multiline[lineindex] = line1;
  }

  private void Concat()
  {
    for (int index1 = 0; index1 < this.multiline.Count; ++index1)
    {
      for (int index2 = 0; index2 < this.multiline[index1].Items.Count - 1; ++index2)
      {
        if (this.multiline[index1].Items[index2].ReturnConditionType() == "const" && this.multiline[index1].Items[index2].ReturnConditionType() == this.multiline[index1].Items[index2 + 1].ReturnConditionType() && this.multiline[index1].Items[index2] is Item)
        {
          this.multiline[index1].Items[index2].Value += this.multiline[index1].Items[index2 + 1].Value;
          this.multiline[index1].Items.RemoveAt(index2 + 1);
        }
      }
    }
  }

  public void AutomaticSplitting(RichTextBox richTextBox)
  {
    this.multiline.Clear();
    int index = 0;
    foreach (string line in richTextBox.Lines)
    {
      this.multiline.Add(new Line());
      foreach (string separateItem in line.SeparateItems(this._params, this.dynamicParameters))
      {
        if (this._params.Contains(separateItem))
          this.multiline[index].Items.Add((ISublines) new Item("parameter")
          {
            Value = separateItem
          });
        else if (this.dynamicParameters.Contains(separateItem))
          this.multiline[index].Items.Add((ISublines) new Item("dynamicParameter")
          {
            Value = separateItem
          });
        else
          this.multiline[index].Items.Add((ISublines) new Item("const")
          {
            Value = separateItem
          });
      }
      ++index;
    }
    this.Concat();
    this.ResetDetailledCommandAndNode();
  }
}
